package com.fjnldx.mes.service.energy;

import com.fjnldx.mes.domain.domainReq.TimeSlot;
import com.fjnldx.mes.domain.domainReq.TimeSlotFour;
import com.fjnldx.mes.domain.domainReq.TimeSlotThree;
import com.fjnldx.mes.domain.domainReq.TimeSlotTwo;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.mapper.energy.EnergyNewMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Service
public class EnergyNewService {

    @Resource
    EnergyNewMapper energyNewMapper;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询各工序电耗汇总
     *
     * @param params
     * @return
     */
    public Result querySummaryOfPowerConsumptionOfProcess(TimeSlot params) {
        LocalDate startTime = params.getStartTime();
        LocalDate endTime = params.getEndTime();
        String cj = params.getCj();
        String bc = params.getBc();
        //Integer day = getDay(startTime, endTime);
        //logger.info("-----天数-----" + day);
        List<Map> maps = energyNewMapper.querySummaryOfPowerConsumptionOfProcess(startTime, endTime, cj, bc);
        Map map = summaryOfPowerConsumptionOfProcessFilter(maps);
        return Result.success(map);
    }

    /**
     * 查询电耗汇总-细纱-自络圆环
     *
     * @param params
     * @return
     */
    public Result querySummaryOfRingXsAndLt(TimeSlot params) {
        LocalDate startTime = params.getStartTime();
        LocalDate endTime = params.getEndTime();
        String cj = params.getCj();
        String bc = params.getBc();
        //Integer day = getDay(startTime, endTime);
        //logger.info("-----天数-----" + day);
        List<Map> maps = energyNewMapper.querySummaryOfRingXsAndLt(startTime, endTime, cj, bc);
        Map result = summaryOfRingXsAndLtFilter(maps);
        return Result.success(result);
    }

    /**
     * 工序电耗柱状图和表格
     *
     * @param params
     * @return
     */
    public Result queryProcessPowerConsumption(TimeSlotTwo params) {
        List<Map> result = energyNewMapper.queryProcessPowerConsumption(params);
        return Result.success(result);
    }

    /**
     * 工序电耗折线图
     *
     * @param params
     * @return
     */
    @Transactional
    public Result queryProcessPowerConsumptionLineChart(TimeSlotTwo params) {
        List<Map> result = energyNewMapper.queryProcessPowerConsumptionLineChart(params);
        return Result.success(result);
        //String startTime = params.getStartTime().toString();
        //Integer day = getDay(params.getStartTime(), params.getEndTime());
        //logger.info("-----天数-----" + day);
        //if (day < 1) {
        //    result = energyNewMapper.queryProcessPowerConsumptionLineChart(params);
        //} else {
        //    result = energyNewMapper.queryProcessPowerConsumptionLineChartDay(params);
        //}
        //if (result.size() > 0) {
        //    ExecutorService service = Executors.newFixedThreadPool(200);
        //    for (Map map : result) {
        //        //String cj = map.get("cj").toString();
        //        String gongxu = map.get("gongxu").toString();
        //        String endTime = map.get("time").toString();
        //        service.execute(new Thread(() -> {
        //            Map ImpEpSum = energyNewMapper.queryProcessPowerConsumptionLineChartImpEpSum(gongxu, startTime, endTime);
        //            map.putAll(ImpEpSum);
        //        }));
        //    }
        //    service.shutdown();
        //    service.awaitTermination(1, TimeUnit.DAYS);
        //    return Result.success(result);
        //}
    }

    /**
     * 品种电耗折线图
     *
     * @param params
     * @return
     */
    @Transactional
    public Result queryProcessPowerConsumptionLineChartPz(TimeSlotThree params) {
        List<Map>  result = energyNewMapper.queryProcessPowerConsumptionLineChartPz(params);
        return Result.success(result);
    }

    /**
     * 品种电耗柱状图和表格
     *
     * @param params
     * @return
     */
    public Result queryProcessPowerConsumptionPz(TimeSlotThree params) {
        List<Map> result = energyNewMapper.queryProcessPowerConsumptionPz(params);
        return Result.success(result);
    }

    /**
     * 机台电明细耗柱状图和表格
     *
     * @param params
     * @return
     */
    public Result queryProcessPowerConsumptionDetail(TimeSlotFour params) {
        List<Map> result = energyNewMapper.queryProcessPowerConsumptionDetail(params);
        return Result.success(result);
    }

    /**
     * 机台电明细耗折线图
     *
     * @param params
     * @return
     */
    public Result queryProcessPowerConsumptionDetailLineChart(TimeSlotFour params) throws InterruptedException {
        List<Map> result;
        String startTime = params.getStartTime().toString();
        Integer day = getDay(params.getStartTime(), params.getEndTime());
        logger.info("-----天数-----" + day);
        if (day < 1) {
            result  = energyNewMapper.queryProcessPowerConsumptionDetailLineChart(params);
        } else {
            result = energyNewMapper.queryProcessPowerConsumptionDetailLineChartDay(params);
        }
        if (result.size() > 0) {
            ExecutorService service = Executors.newFixedThreadPool(200);
            for (Map map : result) {
                String pz = map.get("pz").toString();
                String gongxu = map.get("gongxu").toString();
                String endTime = map.get("time").toString();
                service.execute(new Thread(() -> {
                    Map ImpEpSum = energyNewMapper.queryProcessPowerConsumptionLineChartImpEpSumDetail(gongxu,pz, startTime, endTime);
                    map.putAll(ImpEpSum);
                }));
            }
            service.shutdown();
            service.awaitTermination(1, TimeUnit.DAYS);
            return Result.success(result);
        }
        return Result.error("系统异常");
    }

    public Integer getDay(LocalDateTime startTime, LocalDateTime endTime) {
        LocalDate startLocalDate = LocalDate.of(startTime.getYear(), startTime.getMonth(), startTime.getDayOfMonth());
        LocalDate endLocalDate = LocalDate.of(endTime.getYear(), endTime.getMonth(), endTime.getDayOfMonth());
        Integer day = Math.toIntExact(endLocalDate.toEpochDay() - startLocalDate.toEpochDay());
        return day;
    }

    public Map summaryOfPowerConsumptionOfProcessFilter(List<Map> params) {
        Double sum = 0.0;
        ArrayList<String> qf = new ArrayList<>();
        qf.add("多仓");
        qf.add("抓包");
        qf.add("梳棉");
        qf.add("粗纱");
        qf.add("精开");
        qf.add("并条");
        List<Map> objects = new ArrayList<>();
        List<Map> objectsQf = new ArrayList<>();
        for (Map param : params) {
            String gongxu = param.get("gongxu").toString();
            if (qf.contains(gongxu)) {
                objectsQf.add(param);
                sum += Double.parseDouble(param.get("sumImpEp").toString());
            } else {
                objects.add(param);
            }
        }
        Map<String, Object> qfMap = new HashMap<>();
        qfMap.put("gongxu", "前纺");
        qfMap.put("sumImpEp", sum);
        objects.add(qfMap);
        Map<String, Object> result = new HashMap<>();
        result.put("header", objects);
        result.put("ringQf", objectsQf);
        return result;
    }

    public Map summaryOfRingXsAndLtFilter(List<Map> params) {
        List<Map> objectsXs = new ArrayList<>();
        List<Map> objectsLt = new ArrayList<>();
        for (Map param : params) {
            String gongxu = param.get("gongxu").toString();
            if ("细纱".equals(gongxu)) {
                objectsXs.add(param);
            } else {
                objectsLt.add(param);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("ringXs", objectsXs);
        result.put("ringLt", objectsLt);
        return result;
    }

}
